using System;
using System.Linq;
using DNT.RavenQA.Core.Extensions;
using DNT.RavenQA.Core.Models;
using Raven.Client;
using Raven.Client.Linq;

namespace DNT.RavenQA.Web.Infrastructure.QueryModel
{
    public class AnswersByQuestionQuery : IParametrizedQuery<Answer, Question>
    {
        public IQueryable<Answer> ExecuteWithStats(IDocumentSession session, Question parameter, out RavenQueryStatistics stats)
        {
            parameter.EnsureIsNotNull("parameter");

            return session.Query<Answer>()
                .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite(TimeSpan.FromSeconds(2)))
                .Include(x => x.User.Id)
                .Include(x => x.VotesId)
                .Statistics(out stats)
                .Where(a => a.QuestionId == parameter.Id);
        }

        public IQueryable<Answer> Execute(IDocumentSession session, Question parameter)
        {
            parameter.EnsureIsNotNull("parameter");

            return session.Query<Answer>()
                .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite(TimeSpan.FromSeconds(2)))
                .Include(x => x.User.Id)
                .Include(x => x.VotesId)
                .Where(a => a.QuestionId == parameter.Id);
        }
    }
}